<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>websocket</title>
</head>
<body>
<input type="button" value="运行" onclick="webSocket()"/>
<script>
    function webSocket(){
        if("WebSocket" in window){
            console.log("支持");
            //打开一个websocket
            var ws=new WebSocket("ws://www.easy-mock.com/mock/5a61b18441d8910ea886ec89/example/content");
            ws.onopen=function(){
                alert("发送中...");
            };

            ws.onmessage=function(evt){
                var re=evt.data;
                alert("数据已接受");
            };

            ws.onclose=function(){
                //关闭websocket
                alert("连接已关闭");
            }
        }else{
            //不支持
            alert("不支持")
        }
    }
</script>
</body>
</html>